using Microsoft.EntityFrameworkCore;
using WebApiFirst;
using WebApiFirst.Common;
using WebApiFirst.IService;
using WebApiFirst.Service;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.

builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddDbContext<AppDbContext>(p => p.UseLoggerFactory(LoggerFactory.Create(p=>p.AddConsole())).UseSqlServer(builder.Configuration.GetConnectionString("SqlServer")));
builder.Services.AddAutoMapper(typeof(AutoProfile).Assembly);
builder.Services.AddScoped<IStudentService,StudentService>();
builder.Services.AddLogging(logbuilder=>logbuilder.AddConsole());
var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

app.UseAuthorization();

app.MapControllers();


//使用map为指定路由的action添加中间件，以下就是一个管道
app.Map("/api/Student/middleWare", appbuilders =>
{
    //第一个中间件
    appbuilders.Use(async (context, next) =>
    {
        context.Request.Headers.Add("head3", "33");
        await next.Invoke();
        Console.WriteLine(context.Request.Headers["head3"]);
        Console.WriteLine("44");
    });
    //第二个中间件
    appbuilders.Use(async (context, next) =>
    {
        context.Request.Headers.Add("head5", "55");
        await next.Invoke();
        Console.WriteLine(context.Request.Headers["head5"]);
        Console.WriteLine("66");
    });

    //第三个中间件，也是终止
    appbuilders.Run(async context =>
    {
        Console.WriteLine("/FilterAttribute这个路径下的中间件结束了");
    });
});
app.Run();
